/**
 @file sys_usw_mchip.c

 @date 2017-9-4

 @version v1.0

 The file define APIs of chip of sys layer
*/
/****************************************************************************
 *
 * Header Files
 *
 ****************************************************************************/
#include "ctc_error.h"
#include "ctc_acl.h"
#include "sys_usw_mchip.h"
#include "dal.h"

sys_usw_mchip_t *p_sys_mchip_master[CTC_MAX_LOCAL_CHIP_NUM_PP] = {NULL};
#ifdef DUET2
extern int32 sys_duet2_mchip_init(uint8 lchip);
extern int32 sys_duet2_mchip_deinit(uint8 lchip);
#endif


#ifdef TSINGMA
extern int32 sys_tsingma_mchip_init(uint8 lchip);
extern int32 sys_tsingma_mchip_deinit(uint8 lchip);
#endif

#if defined(TSINGMA_MX)
extern int32 sys_tmm_mchip_init(uint8 lchip);
extern int32 sys_tmm_mchip_deinit(uint8 lchip);
#endif
#if defined(TSINGMA_GX)
extern int32 sys_tmg_mchip_init(uint8 lchip);
extern int32 sys_tmg_mchip_deinit(uint8 lchip);
#endif

#ifdef ARCTIC
extern int32 sys_at_mchip_init(uint8 lchip);
extern int32 sys_at_mchip_deinit(uint8 lchip);
#endif

int32
sys_usw_mchip_init(uint8 lchip)
{
    uint32 dev_id = 0;
    dal_get_chip_dev_id(SYS_MAP_LDEV(lchip), &(dev_id));

    /* 2. allocate interrupt master */
    if (p_sys_mchip_master[lchip])
    {
        return CTC_E_NONE;
    }
    if (SYS_VCHIP_PP_IS_NOT_BASE(lchip))
    {
        p_sys_mchip_master[lchip] = p_sys_mchip_master[drv_vchip_get_pp_base(lchip)];
        return CTC_E_NONE;
    }

    p_sys_mchip_master[lchip] = (sys_usw_mchip_t*)mem_malloc(MEM_SYSTEM_MODULE, sizeof(sys_usw_mchip_t));
    if (NULL == p_sys_mchip_master[lchip])
    {
        return CTC_E_NO_MEMORY;
    }

    sal_mutex_create(&p_sys_mchip_master[lchip]->lag_block_mutex);
    if (NULL == p_sys_mchip_master[lchip]->lag_block_mutex)
    {
        return CTC_E_NO_MEMORY;
    }
    sal_memset(p_sys_mchip_master[lchip]->sub_feature,0X01 ,SYS_SUB_FEATURE_MAX*sizeof(uint8));
#ifdef DUET2
    if (dev_id == DAL_DUET2_DEVICE_ID)
    {
        sys_duet2_mchip_init(lchip);
    }
#endif


#ifdef TSINGMA
    if (dev_id == DAL_TSINGMA_DEVICE_ID)
    {
        sys_tsingma_mchip_init(lchip);
    }
#endif

#if defined(TSINGMA_MX)
    if (dev_id == DAL_TSINGMA_MX_DEVICE_ID)
    {
        sys_tmm_mchip_init(lchip);
    }
#endif
#if defined(TSINGMA_GX)
    if (dev_id == DAL_TSINGMA_GX_DEVICE_ID)
    {
        sys_tmg_mchip_init(lchip);
    }
#endif

#ifdef ARCTIC
    if (dev_id == DAL_ARCTIC_DEVICE_ID)
    {
        sys_at_mchip_init(lchip);
    }
#endif

    MCHIP_MISC(lchip)->mchip_cap_init(lchip);

    return CTC_E_NONE;
}

int32 sys_usw_mchip_set_feature_en(uint8 lchip, uint8 feature)
{
    if (!p_sys_mchip_master[lchip])
    {
       return CTC_E_NOT_INIT;
    }
    CTC_BIT_SET(p_sys_mchip_master[lchip]->feature[feature], SYS_FEATURE_EN);
    return CTC_E_NONE;
}

int32 sys_usw_mchip_set_feature_pp_en(uint8 lchip, uint8 feature)
{
    if (!p_sys_mchip_master[lchip])
    {
       return CTC_E_NOT_INIT;
    }
    CTC_BIT_SET(p_sys_mchip_master[lchip]->feature[feature], SYS_FEATURE_PP_EN);
    return CTC_E_NONE;
}

int32
sys_usw_mchip_deinit(uint8 lchip)
{
    uint32 dev_id = 0;

    if (!p_sys_mchip_master[lchip])
    {
        return CTC_E_NONE;
    }
    if (SYS_VCHIP_PP_IS_NOT_BASE(lchip))
    {
        p_sys_mchip_master[lchip] = NULL;
        return CTC_E_NONE;
    }

    dal_get_chip_dev_id(SYS_MAP_LDEV(lchip), &dev_id);
    switch (dev_id)
    {

    #ifdef DUET2
        case DAL_DUET2_DEVICE_ID:
            sys_duet2_mchip_deinit(lchip);
            break;
    #endif

    #ifdef TSINGMA
        case DAL_TSINGMA_DEVICE_ID:
            sys_tsingma_mchip_deinit(lchip);
            break;
    #endif

    #if defined(TSINGMA_MX)
            case DAL_TSINGMA_MX_DEVICE_ID:
                sys_tmm_mchip_deinit(lchip);
                break;
    #endif
    #if defined(TSINGMA_GX)
            case DAL_TSINGMA_GX_DEVICE_ID:
                sys_tmg_mchip_deinit(lchip);
                break;
    #endif

    #ifdef ARCTIC
            case DAL_ARCTIC_DEVICE_ID:
                sys_at_mchip_deinit(lchip);
                break;
    #endif
        default:
            return -1;
            break;
    }
    if (p_sys_mchip_master[lchip]->lag_block_mutex)
    {
        sal_mutex_destroy(p_sys_mchip_master[lchip]->lag_block_mutex);
    }
    mem_free(p_sys_mchip_master[lchip]);

    return CTC_E_NONE;
}

